package com.fasteat.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasteat.common.entity.Result;
import com.fasteat.mall.entity.Logistics;
import com.fasteat.mall.service.LogisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hy
 * @since 2021-01-19
 */
@RestController
@CrossOrigin
@Api(description = "物流接口")
@RequestMapping("/mall/logistics")
public class LogisticsController {
    @Autowired
    private LogisticsService logisticsService;

    /**
     * 新增物流
     * @param logistics
     * @return
     */
    @ApiOperation(value = "新增物流信息")
    @PostMapping
    public Result save(@RequestBody Logistics logistics) {
        if(logistics == null) {
            return Result.error("请勿添加空物流");
        }
        boolean result = logisticsService.save(logistics);
        return result ?
                Result.success("添加成功")
                :
                Result.error("添加失败");
    }

    /**
     * 根据物流id删除物流
     * @param logisticsId
     * @return
     */
    @DeleteMapping(value = "/delete/{orderFormId}")
    @ApiOperation(value = "根据物流id删除物流")
    public Result deleteById(@PathVariable(name = "orderFormId", required = true)
                             @ApiParam(value = "orderFormId", required = true)
                                     String logisticsId) {
        if (logisticsId == null) {
            return Result.error("删除失败");
        }
        boolean result = logisticsService.removeById(logisticsId);
        return result ?
                Result.success("删除成功")
                :
                Result.error("删除失败");
    }

    /**
     * 修改物流信息
     * @param logistics
     * @return
     */
    @ApiOperation(value = "根据物流id修改物流信息")
    @PutMapping
    public Result updateProduct(@RequestBody
                                @ApiParam(value = "物流Logistics对象", required = true)
                                            Logistics logistics) {
        if (StringUtils.isEmpty(logistics.getId())) {
            return Result.error("修改失败");
        }
        boolean result = logisticsService.updateById(logistics);
        return result
                ?
                Result.success("修改成功")
                :
                Result.error("修改失败");
    }

    /**
     * 分页查看物流
     *
     * @return
     */
    @GetMapping("/{size}/{current}")
    @ApiOperation("分页查看物流")
    public Result acceptProduct(@PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
                                @PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current) {
        size = size <= 0 ? 5 : size;
        current = current <= 0 ? 1 : current;
        Page<Logistics> page = new Page(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("gmt_create");
        logisticsService.page(page, wrapper);
        return Result.success().data(page);
    }

    /**
     * 通过物流ID获取物流信息
     *
     * @param logisticsId
     * @return
     */
    @GetMapping("findById/{logisticsId}")
    @ApiOperation("通过物流ID获取物流信息")
    public Result findById(@PathVariable(name = "logisticsId", required = true)
                           @ApiParam(value = "物流ID", required = true)
                                   String logisticsId) {
        if (StringUtils.isEmpty(logisticsId)) {
            return Result.error("查询失败");
        }
        // 从数据库中查询商品的信息
        Logistics logistics = logisticsService.getById(logisticsId);
        return Result.success("查询成功").data(logistics);
    }
}